package com.alex.controller;

import cn.hutool.http.HttpUtil;
import com.alex.entityVo.ArticleSearchVo;
import com.alex.sevice.StaticService;
import com.alibaba.fastjson2.JSONObject;
import common.beans.ResultBean;
import common.beans.User;
import common.util.UserUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RequestMapping("/search")
@RestController
@Tag(name = "核心检索")
public class MainController {


    private static final Logger log = LoggerFactory.getLogger(MainController.class);
    public static String RESTURL_ES = "http://10.1.1.11:29000";

    @Resource
    StaticService staticService;

    @Operation(summary = "检索", description = "第一个筛选, 输入检索词, 返回左侧文章列表")
    @PostMapping(value = "search")
    public String search(@RequestBody ArticleSearchVo articleSearchVo) {
        log.info("articleSearchVo+" + articleSearchVo);
        User user = UserUtil.getUserUnloginDeniey();
        articleSearchVo.setUid(user.getUid());
        articleSearchVo.setGid(user.getGid());
        articleSearchVo.setRealName(user.getRealname());
        staticService.insStat(articleSearchVo.getPram(),"检索");

        return HttpUtil.post(RESTURL_ES + "/search/search", JSONObject.toJSONString(articleSearchVo));
    }

    @Operation(summary = "根据检索结果返回的 UUID 来生成柱状图", description = "")
    @GetMapping(value = "chart_bar")
    public String chart_bar(String uuid, String chartType) {
        Map<String, Object> map = new HashMap<>();
        map.put("uuid", uuid);
        map.put("type", chartType);
        System.out.println("uuid:" + uuid);
        System.out.println("chartType:" + chartType);
        return HttpUtil.post(RESTURL_ES + "/search/chart_bar", map);
    }

    @Operation(summary = "热词", description = "")
    @GetMapping(value = "research_hotspots")
    public String research_hotspots(String uuid) {
        Map<String, Object> map = new HashMap<>();
        map.put("uuid", uuid);
        System.out.println("uuid:" + uuid);
        return HttpUtil.post(RESTURL_ES + "/cn_pubmed/research_hotspots", map);
    }

    @Operation(summary = "作者合作网络", description = "")
    @GetMapping(value = "auth_net_work")
    public String auth_net_work(String uuid) {
        Map<String, Object> map = new HashMap<>();
        map.put("uuid", uuid);
        System.out.println("uuid:" + uuid);
        return HttpUtil.post(RESTURL_ES + "/cn_pubmed/auth_net_work", map);
    }


    @Operation(summary = "查看具体的文章", description = "如果预警期刊列表的count 不等于0 则展示预警文章列表")
    @GetMapping(value = "waring_article")
    public String waring_article(
            @Parameter(name = "jid", description = "期刊id", example = "1", required = true) Integer jid,
            @Parameter(name = "page", description = "当前页", example = "1", required = true) Integer page,
            @Parameter(name = "limit", description = "每页显示条数", example = "10", required = true) Integer limit) {
        Map<String, Object> map = new HashMap<>();
        map.put("jid", jid);
        map.put("page", page);
        map.put("limit", limit);
        return HttpUtil.get(RESTURL_ES + "/cn_pubmed/waring_article", map);
    }


    @GetMapping("trans")
    public String translate(Integer id) {
        log.info("准备翻译" + id);
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        return HttpUtil.post(RESTURL_ES + "/cn_pubmed/translate", map);
    }


}
